import pygame

pygame.init()
blocklong = 16
w, h = screen_size = (31 * blocklong, 21 * blocklong)
screen = pygame.display.set_mode(screen_size)
Run = True


# 1个方块需要 <坐标> <材质> <形态(固\液\气\虚无态)> <碰撞箱坐标角1> <碰撞箱坐标角2>
class Block:
    def __init__(self, pos: tuple[int, int], texture: str, morphology: int,
                 hitbox_pos1: tuple[int, int], hitbox_pos2: tuple[int, int]):
        # 这里的参数与上文一一对应, 其中 morphology 表示的是形态, 我们规定1为固态, 2为液态, 3为气态
        self.x, self.y = self.pos = pos
        self.texture = pygame.image.load(texture)
        self.morphology = morphology
        self.hitbox = pygame.Rect(hitbox_pos1[0], hitbox_pos1[1], hitbox_pos2[0], hitbox_pos2[1])


# 这是实体的相关代码
# noinspection PyProtectedMember
class Entity:
    def __init__(self, pos: tuple[int, int], texture: str, type: int, ticks: int = 0):
        # 目前我还没想好具体的变量, 就写了点基础的(不可或缺的)变量awa
        # type 表示类型, 1为玩家, 2为其他实体
        self.x, self.y = self.pos = pos
        self.texture = pygame.image.load(texture)
        self.hitbox = Hitbox((pos[0], pos[1]), (pos[0] + blocklong, pos[1] + blocklong))
        self.type = type
        self.ticks = ticks

    def _move(self, x, y):
        # 这是实体移动的代码, x和y是相对于实体的坐标
        if w - 16 >= self.x + x >= 0:
            self.x += x
        if h - 16 >= self.y + y >= 0:
            self.y += y
        self.pos = self.x, self.y
        can = True
        for block in blocklist:
            block: Block
            if self.pos == block.pos and block.morphology == 1:
                can = False
                break
        if not can:
            self.x -= x
            self.y -= y
            self.pos = self.x, self.y
            x = y = 0
        self.hitbox._move(x, y)
        if not (self.hitbox.pos1 == self.pos and self.hitbox.pos2 == (self.x + blocklong, self.y + blocklong)):
            self.hitbox._move(self.x - self.hitbox.x1, self.y - self.hitbox.y1)

    def ai(self):
        if self.type != 1:
            pass  # ...
        else:
            return

    def go(self, event):
        return


class Hitbox:
    def __init__(self, pos1: tuple[int, int], pos2: tuple[int, int]):
        self.pos1 = pos1
        self.pos2 = pos2
        self.x1 = pos1[0]  # 左
        self.y1 = pos1[1]  # 上
        self.x2 = pos2[0]  # 右
        self.y2 = pos2[1]  # 下

    def pz(self, hitbox) -> bool:
        return ((hitbox.x1 <= self.x2 <= hitbox.x2 and hitbox.y1 <= self.y1 <= hitbox.y2) or
                (hitbox.x1 <= self.x1 <= hitbox.x2 and hitbox.y1 <= self.y2 <= hitbox.y2))

    def _move(self, x, y):
        self.x1 += x
        self.x2 += x
        self.y1 += y
        self.y2 += y
        self.pos1 = self.x1, self.y1
        self.pos2 = self.x2, self.y2


def regBlock(block: Block):
    blocklist.append(block)


def regFunc(f):
    functionList.append(f)


def showBG(screen):
    pass


def showUI(screen):
    pass


def setVar(var: int, value: bool):
    if var == 1:
        global showBlock
        showBlock = value
    elif var == 2:
        global showEntity
        showEntity = value
    elif var == 3:
        global runFunc
        runFunc = value


showBlock = False
showEntity = False
runFunc = False

entitylist = []
blocklist = []
functionList = []
